﻿/* AgileFx Version 2.0
 * An open-source framework for rapid development of applications and services using Microsoft.net
 * Developed by: AgileHead
 * Website: www.agilefx.org
 * This component is licensed under the terms of the Apache 2.0 License.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Mvc.Html;

namespace AgileFx.MVC.Controls
{
    public static partial class HtmlUtil
    {
        public static MvcForm BeginValidatedForm(this HtmlHelper htmlHelper)
        {
            var formAction = htmlHelper.ViewContext.HttpContext.Request.RawUrl;
            return htmlHelper.BeginForm("", "", FormMethod.Post, GetFormValidationAttributes(new { action = formAction }));
        }

        public static MvcForm BeginValidatedForm(this HtmlHelper htmlHelper, object htmlAttributes)
        {
            var attrs = GetFormValidationAttributes(htmlAttributes);
            attrs["action"] = htmlHelper.ViewContext.HttpContext.Request.RawUrl;
            return htmlHelper.BeginForm("", "", FormMethod.Post, attrs);
        }

        public static MvcForm BeginValidatedForm(this HtmlHelper htmlHelper, string actionName, string controllerName)
        {
            return htmlHelper.BeginForm(actionName, controllerName, FormMethod.Post, GetFormValidationAttributes(null));
        }

        public static MvcForm BeginValidatedForm(this HtmlHelper htmlHelper, string actionName,
            string controllerName, object routeValues, FormMethod method, object htmlAttributes)
        {
            return htmlHelper.BeginForm(actionName, controllerName, new RouteValueDictionary(routeValues), method, GetFormValidationAttributes(htmlAttributes));
        }
    }
}
